<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="query.fundCode" clearable size="small" placeholder="基金代码" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-select
          v-model="query.direction"
          clearable
          size="small"
          class="filter-item"
          placeholder="操作类型"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in directionOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <date-range-picker v-model="query.operateDate" class="date-item" />

        <rrOperation />
      </div>
      <crudOperation :permission="permission" />
    </div>
    <!--表单渲染-->
    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible.sync="crud.status.cu > 0"
      :title="crud.status.title"
      width="520px"
    >
      <el-form
        ref="form"
        :inline="true"
        :model="form"
        :rules="rules"
        size="small"
        label-width="80px"
      >
        <el-form-item
          label="基金编码"
          prop="fundCode"
        >
          <el-input
            v-model="form.fundCode"
            style="width:370px;"
          />
        </el-form-item>
        <el-form-item
          label="基金名称"
          prop="fundName"
        >
          <el-input
            v-model="form.fundName"
            style="width:370px;"
            :disabled="true"
          />
        </el-form-item>
        <el-form-item
          label="操作方向"
          prop="direction"
        >
          <el-select
            v-model="form.direction"
            style="width: 140px"
            controls-position="right"
            placeholder="请选择"
          >
            <el-option
              v-for="item in directionOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="操作日期"
          prop="operateDate"
        >
          <el-date-picker
            v-model="form.operateDate"
            type="date"
            style="width: 140px"
            placeholder="选择日期"
          />
        </el-form-item>
        <el-form-item
          label="操作金额"
          prop="operateAmount"
        >
          <el-input
            v-model="form.operateAmount"
            style="width: 370px"
          />
        </el-form-item>
        <el-form-item
          label="服务费"
          prop="serviceFee"
        >
          <el-input
            v-model="form.serviceFee"
            style="width: 140px"
          />
        </el-form-item>
        <el-form-item
          label="成交份额"
          prop="operateCount"
        >
          <el-input
            v-model="form.operateCount"
            style="width: 140px"
          />
        </el-form-item>
        <el-form-item
          label="成交净值"
          prop="confirmNetValue"
        >
          <el-input
            v-model="form.confirmNetValue"
            style="width: 140px"
          />
        </el-form-item>
        <el-form-item
          label="成交日期"
          prop="confirmDate"
        >
          <el-date-picker
            v-model="form.confirmDate"
            type="date"
            style="width: 140px"
            placeholder="成交日期"
          />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="crud.cancelCU">取消</el-button>
        <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
      </div>
    </el-dialog>
    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      row-key="id"
      :row-class-name="tableRowClassName"
      @selection-change="crud.selectionChangeHandler"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column :show-overflow-tooltip="true" label="基金代码" prop="fundCode" />

      <el-table-column :show-overflow-tooltip="true" width="180" prop="fundName" label="基金名称" />
      <el-table-column :show-overflow-tooltip="true" prop="direction" label="操作方向">
        <template slot-scope="scope">
          <span v-if="scope.row.fundName === '合计'" />
          <el-tag v-else-if="scope.row.direction === 0" class="danger">卖出</el-tag>
          <el-tag v-else-if="scope.row.direction === 1" class="success">买入</el-tag>
        </template>
      </el-table-column>

      <el-table-column :show-overflow-tooltip="true" prop="operateDate" label="操作时间">
        <template slot-scope="scope">
          <span>{{ !scope.row.operateDate ? '' : scope.row.operateDate | formatDate }}</span>
        </template>
      </el-table-column>

      <el-table-column :show-overflow-tooltip="true" prop="operateAmount" label="操作金额">
        <template slot-scope="scope">
          <span v-if="scope.row.operateAmount > 0" style="color: red;">{{ !scope.row.operateAmount ? 0 : scope.row.operateAmount | rounding | thousands }}</span>
          <span v-else style="color: green;">{{ !scope.row.operateAmount ? 0 : scope.row.operateAmount | rounding | thousands }}</span>
        </template>
      </el-table-column>

      <el-table-column :show-overflow-tooltip="true" prop="serviceFee" label="手续费">
        <template slot-scope="scope">
          <span>{{ !scope.row.serviceFee ? 0 : scope.row.serviceFee | rounding }}</span>
        </template>
      </el-table-column>

      <el-table-column :show-overflow-tooltip="true" prop="operateCount" label="成交金额/净值">
        <template slot-scope="scope">
          <span>{{ scope.row.operateCount }}</span>
          <br>
          <span>{{ scope.row.confirmNetValue }}</span>
        </template>
      </el-table-column>

      <el-table-column :show-overflow-tooltip="true" prop="status" label="成交状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1" type="success">交易成功</el-tag>
          <el-tag v-else-if="scope.row.status === 2" type="danger">待确认</el-tag>
          <br>
          <span v-if="scope.row.status === 1">{{ scope.row.confirmDate | formatDate }}</span>
        </template>
      </el-table-column>

      <el-table-column
        v-if="checkPer(['admin','userfund:edit','userfund:del'])"
        label="操作"
        width="130px"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <udOperation
            v-if="scope.row.fundName !== '合计'"
            :data="scope.row"
            :permission="permission"
          />
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination />
  </div>
</template>

<script>
import crudUserFundFlow from '@/api/lifetools/userfundflow'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { dateFormat, thousands } from '@/utils/index'
import DateRangePicker from '@/components/DateRangePicker'

// crud交由presenter持有
const defaultForm = {
  id: null,
  fundCode: null,
  fundName: null,
  direction: 1,
  operateDate: new Date(),
  operateAmount: null,
  serviceFee: null,
  operateCount: null,
  confirmNetValue: null,
  status: null,
  confirmDate: null
}
export default {
  name: 'UserFundFlow',
  components: { crudOperation, rrOperation, udOperation, pagination, DateRangePicker },
  cruds() {
    return CRUD({
      title: '基金操作纪录',
      url: 'api/userfundflow',
      optShow: {
        add: true,
        edit: true,
        del: true,
        download: false,
        reset: true
      },
      sort: ['operateDate,desc', 'id,desc'],
      crudMethod: { ...crudUserFundFlow }
    })
  },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  data() {
    return {
      rules: {
        fundCode: [
          { required: true, message: '请输入基金编码', trigger: 'blur' }
        ],
        direction: [
          { required: true, message: '请输入操作方向', trigger: 'blur' }
        ],
        operateDate: [
          { required: true, message: '请输入操作日期', trigger: 'blur' }
        ],
        operateAmount: [
          { required: true, message: '请输入操作金额', trigger: 'blur' }
        ]
      },
      directionOptions: [{
        value: 0,
        label: '卖出'
      }, {
        value: 1,
        label: '买入'
      }],
      permission: {
        add: ['admin', 'userfundflow:add'],
        edit: ['admin', 'userfundflow:edit'],
        del: ['admin', 'userfundflow:del']
      }
    }
  },
  filters: {
    rounding(value) {
      return value.toFixed(2)
    },
    thousands(value) {
      return thousands(value)
    },
    formatDate(value) {
      if (!value) {
        return ''
      }
      return dateFormat(new Date(value), 'yyyy-mm-dd')
    }
  },
  methods: {
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex === 0) {
        return 'warning-row'
      }
      return ''
    },
    // 选中图标
    selected(name) {
      this.form.icon = name
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  ::v-deep .el-table .warning-row {
    background: #E6A23C;
  }
 ::v-deep .el-input-number .el-input__inner {
    text-align: left;
  }
 ::v-deep .vue-treeselect__control,::v-deep .vue-treeselect__placeholder,::v-deep .vue-treeselect__single-value {
    height: 30px;
    line-height: 30px;
  }
</style>
